package com.example.azheng.okhttpdemo;

import android.arch.lifecycle.Lifecycle;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.azheng.okhttpdemo.adapter.RclyAdapter;
import com.example.azheng.okhttpdemo.base.BaseActivity;
import com.example.azheng.okhttpdemo.beans.JuheBean;
import com.example.azheng.okhttpdemo.contract.JuheNewContract;
import com.example.azheng.okhttpdemo.presenter.JuheNewPresenter;
import com.example.azheng.okhttpdemo.utils.GlideApp;
import com.example.azheng.okhttpdemo.utils.GlideImageLoader;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.AutoDisposeConverter;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author azheng
 * @date 2018/4/14.
 * GitHub：https://github.com/RookieExaminer
 * email：wei.azheng@foxmail.com
 * description：
 */
public class MainActivity extends BaseActivity<JuheNewPresenter> implements JuheNewContract.View, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.mRecyclerView_main)
    RecyclerView mRecyclerViewMain;
    @BindView(R.id.mSwipeRefreshLayout_main)
    SwipeRefreshLayout mSwipeRefreshLayoutMain;
    @BindView(R.id.mImage_main)
    ImageView mImageMain;

    private Banner mBanner;

    private RclyAdapter mRclyAdapter;
    private List<JuheBean.ResultBean.DataBean> juheBaenList;

    private boolean isFirst = true;
    private static final String TAG = "MainActivitysss";

    @Override
    public void initView() {
        juheBaenList = new ArrayList<>();
        mRclyAdapter = new RclyAdapter(R.layout.activity_main_item, juheBaenList);
        mSwipeRefreshLayoutMain.setOnRefreshListener(this);
        //解决滑动冲突
        mRecyclerViewMain.setNestedScrollingEnabled(false);
        setmRecyclerView();
        mPresenter = new JuheNewPresenter();
        mPresenter.attachView(this);
        mPresenter.onRefresh();
    }

    private void setmRecyclerView() {
        //瀑布流
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

        mRecyclerViewMain.setLayoutManager(layoutManager);

        //添加头布局
        addHeader();

        mRecyclerViewMain.setAdapter(mRclyAdapter);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void addHeader() {
        View headerView = getHeaderView(0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        View headerView2 = getHeaderView(1, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mRclyAdapter.addHeaderView(headerView);
        mRclyAdapter.addHeaderView(headerView2);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mPresenter.onRefresh();
            }
        }, 1200);
    }

    @Override
    public void showLoading() {
        if (mSwipeRefreshLayoutMain != null && !mSwipeRefreshLayoutMain.isRefreshing()) {
            mSwipeRefreshLayoutMain.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (mSwipeRefreshLayoutMain != null && mSwipeRefreshLayoutMain.isRefreshing()) {
            mSwipeRefreshLayoutMain.setRefreshing(false);
        }
    }

    @Override
    public void showData(JuheBean juheBaen) {

        if (isFirst) {
            Log.d(TAG, "第一次");

            List<String> mListImg = new ArrayList<>();
            mListImg.add("https://imgsa.baidu.com/baike/pic/item/f703738da977391285c47785f3198618377ae270.jpg");
            mListImg.add("https://imgsa.baidu.com/baike/pic/item/58ee3d6d55fbb2fb200ea5f6444a20a44623dc29.jpg");
            mListImg.add("https://imgsa.baidu.com/baike/pic/item/32fa828ba61ea8d31dd086e09c0a304e241f5860.jpg");
            mListImg.add("https://imgsa.baidu.com/baike/pic/item/3bf33a87e950352a4b8fd7ec5843fbf2b3118b6c.jpg");

            GlideApp.with(MainActivity.this).load("https://images.unsplash.com/photo-1492684689425-8012ed8d9408?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=b14513cbd2ee4e1a7f06e3ea28c018b7&auto=format&fit=crop&w=1050&q=80").into(mImageMain);
            mBanner.setImages(mListImg).setImageLoader(new GlideImageLoader()).start();
            isFirst = false;

            mRclyAdapter.addData(juheBaen.getResult().getData());

        } else {
            Log.d(TAG, "第二次");
            mRclyAdapter.setNewData(juheBaen.getResult().getData());

        }


    }

    @Override
    public void getDataFailed() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    private View getHeaderView(int type, View.OnClickListener listener) {
        View view;
        if (type == 0) {
            view = getLayoutInflater().inflate(R.layout.head_view_banner, (ViewGroup) mRecyclerViewMain.getParent(), false);
            mBanner = view.findViewById(R.id.mBanner_item);


        } else {
            view = getLayoutInflater().inflate(R.layout.head_view_type, (ViewGroup) mRecyclerViewMain.getParent(), false);

            TextView textView = view.findViewById(R.id.textView_type);
            textView.setText("分类~~~~");

        }
        view.setOnClickListener(listener);

        return view;
    }
}
